﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using SharpBroadcast;

namespace WebVideoServer
{
    partial class VideoService : ServiceBase
    {
        private IMediaServer m_VideoServer = null;
        private IMediaServer m_AudioServer = null;

        public VideoService()
        {
            InitializeComponent();

            if (m_VideoServer == null) m_VideoServer = new MediaServer<MpegHandler>(CommonLog.GetLogger());
            if (m_AudioServer == null) m_AudioServer = new MediaServer<Mp3Handler>(CommonLog.GetLogger());

            if (m_VideoServer != null) m_VideoServer.SetClientValidator(new WebClientValidator());
            if (m_AudioServer != null) m_AudioServer.SetClientValidator(new WebClientValidator());
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.

            ConfigurationManager.RefreshSection("appSettings");

            CommonLog.Info("=== Media Server is starting ===");

            ServerSetting setting = ServerSetting.Reload(CommonLog.GetLogger(), "Video");

            if (m_VideoServer != null)
            {
                m_VideoServer.OutputBufferSize = setting.OutputBufferSize;
                m_VideoServer.OutputQueueSize = setting.OutputQueueSize;
                m_VideoServer.Start(setting.InputPort, setting.OutputPort, setting.InputWhitelist);

                if (m_VideoServer.IsWorking())
                {
                    CommonLog.Info("Video Server is working on port " + m_VideoServer.InputPort
                        + " (input) and port " + m_VideoServer.OutputPort + " (output) ... ");
                }
            }

            if (m_AudioServer != null)
            {
                bool needAudio = false;
                try
                {
                    var appSettings = ConfigurationManager.AppSettings;
                    var allKeys = appSettings.AllKeys;
                    if (allKeys.Contains("EnableAudio")) needAudio = Convert.ToBoolean(appSettings["EnableAudio"]);
                }
                catch { }

                if (needAudio)
                {
                    ServerSetting audioSetting = ServerSetting.Reload(CommonLog.GetLogger(), "Audio");

                    m_AudioServer.OutputBufferSize = audioSetting.OutputBufferSize;
                    m_AudioServer.OutputQueueSize = audioSetting.OutputQueueSize;

                    m_AudioServer.CacheQueueSize = audioSetting.CacheQueueSize;

                    m_AudioServer.Start(audioSetting.InputPort, audioSetting.OutputPort, setting.InputWhitelist);

                    if (m_AudioServer.IsWorking())
                    {
                        CommonLog.Info("Audio Server is working on port " + m_AudioServer.InputPort 
                            + " (input) and port " + m_AudioServer.OutputPort + " (output) ... ");
                    }
                }
            }
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.

            if (m_VideoServer != null)
            {
                if (m_VideoServer.IsWorking()) m_VideoServer.Stop();
            }

            if (m_AudioServer != null)
            {
                if (m_AudioServer.IsWorking()) m_AudioServer.Stop();
            }

            CommonLog.Info("=== Media Server stopped ===");
        }
    }
}
